package com.polaris.live.ui.vip.fragment

import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.StringUtils
import com.gyf.immersionbar.ImmersionBar
import com.polaris.live.common.adapter.ViewPagerAdapter
import com.polaris.live.common.constant.BizEventConst
import com.polaris.live.common.event.EventMessage
import com.polaris.live.common.mvvm.base2.BaseVbFragment
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.databinding.FragmentVipCenterOwnMainBinding
import com.polaris.live.resp.back_resp.UserVipCenterResp
import com.polaris.live.translation.R
import com.polaris.live.widget.tab.TabBean
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * VipCenterOwnMainFragment
 * @author Created by 天晴 on 2024/12/10 10:40
 * @since 1.0.0
 **/
class VipCenterOwnMainFragment : BaseVbFragment<BaseViewModel, FragmentVipCenterOwnMainBinding>() {

    private val mLevel by lazy { arguments?.getInt("level") ?: -1 }

    private val mFragments = ArrayList<VipCenterOwnFragment>()
    private val mTitles = ArrayList<TabBean>()
    private var mTabAdapter: ViewPagerAdapter? = null

    private var mUserVipCenterResp: UserVipCenterResp? = null

    private var mNowShowLevel: Int = -1

    override fun initView(savedInstanceState: Bundle?) {
        EventBus.getDefault().register(this)
        ImmersionBar.with(this)
            .statusBarDarkFont(false)
            .titleBarMarginTop(mBinding.titleRl)
            .statusBarDarkFont(false)
            .transparentNavigationBar()
            .init()
        mNowShowLevel = mLevel

        mTabAdapter = ViewPagerAdapter(mFragments, this)
        mBinding.viewpager2.adapter = mTabAdapter
        mBinding.tab.setViewPage(mBinding.viewpager2)
        mBinding.goBuyVip.setOnClickListener {
            toVip?.invoke(1)
        }
        mBinding.viewpager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                mNowShowLevel = mFragments[position].mLevel.toInt()
            }
        })
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: EventMessage) {
        when (event.from) {
            BizEventConst.INFO_UPDATA_SUCCESS -> {//主页面更新用户数据完成
                mFragments.forEach {
                    it.upDataUser()
                }
            }
        }
    }

    fun goPageSelected(level: Int) {
        withLoaded {
            val po = mFragments.indexOfFirst { it.mLevel.toInt() == level }
            if (po != -1) {
                mBinding.tab.onPageSelected(po)
                mBinding.viewpager2.setCurrentItem(po, true)
            } else {
                mNowShowLevel = level
            }
        }
    }

    fun setData(mUserVipCenterResp: UserVipCenterResp) {
        this.mUserVipCenterResp = mUserVipCenterResp
        withLoaded {
            mFragments.clear()
            mTitles.clear()
            //是否有vip
            val haveVipList =
                mUserVipCenterResp.levels.filter { it.bizType > 0 }
            if (haveVipList.isNotEmpty()) {
                mBinding.goVipCl.visibility = View.GONE
                mBinding.viewpagerNs.visibility = View.VISIBLE
                mBinding.titleRl.visibility = View.VISIBLE
                var indexCurrent = -1
                haveVipList.forEachIndexed { index, userVipLevel ->
                    mFragments.add(VipCenterOwnFragment().apply {
                        if (mNowShowLevel.toLong() == userVipLevel.level) {
                            indexCurrent = index
                        }
                        arguments = bundleOf(
                            "level" to userVipLevel.level,
                            "data" to userVipLevel,
                        )
                        upData {
                            upData?.invoke()
                        }
                    })
                    mTitles.add(TabBean(StringUtils.getString(R.string.vip_level, userVipLevel.level)))
                }
                mTabAdapter?.setFragments(mFragments)
                mBinding.tab.setTabs(mTitles)
                mBinding.viewpager2.offscreenPageLimit = mFragments.size
                if (indexCurrent != -1) {
                    mBinding.viewpager2.setCurrentItem(indexCurrent, false)
                    mBinding.tab.onPageSelected(indexCurrent)
                } else {
                    mBinding.viewpager2.setCurrentItem(mFragments.size - 1, false)
                    mBinding.tab.onPageSelected(mFragments.size - 1)
                }
            } else {
                mBinding.goVipCl.visibility = View.VISIBLE
                mBinding.viewpagerNs.visibility = View.GONE
                mBinding.titleRl.visibility = View.GONE
            }
        }

    }

    private var upData: (() -> Unit)? = null
    fun upData(back: () -> Unit) {
        this.upData = back
    }

    private var toVip: ((level: Int) -> Unit)? = null
    fun toVip(toVIP: (level: Int) -> Unit) {
        this.toVip = toVIP
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}